package com.lijinquan.tank;

import com.lijinquan.tank.abstractfactory.BaseTank;

import java.awt.*;
import java.util.Random;

/**
 * Created by lijinquan on 2020/3/17.
 */
public class Tank extends BaseTank {

    final int SPPED = 5;

    public static int WIDTH = ResourceMgr.goodTankU.getWidth(), HEIGHT = ResourceMgr.goodTankU.getHeight();


     int x, y,recX,recY;
     Dir dir = Dir.DOWN;

    private boolean moving = true;

    private boolean living = true;

    private Group group = Group.BAD;

    public TankFrame tankFrame = null;

    public Rectangle tankRec=new Rectangle();

    private Random random = new Random();

    FireStrategy fs=null;




    public Tank(int x, int y, Dir dir, Group group, TankFrame tankFrame) {
        this.x = x;
        this.y = y;
        this.dir = dir;
        this.group = group;
        this.tankFrame = tankFrame;

        tankRec.x = this.x;
        tankRec.y = this.y;
        tankRec.width = WIDTH;
        tankRec.height = HEIGHT;

        if(group==Group.GOOD){
            fs=new FourFireStrategy();
        }else {
            fs=new DefultFireStrategy();
        }

    }


    public void paint(Graphics g) {

        if (!living) {
            tankFrame.tanks.remove(this);
        }

        switch (dir) {
            case LEFT:
                g.drawImage(this.getGroup()==Group.GOOD?random.nextInt(100)>50?ResourceMgr.goodTankL:ResourceMgr.goodTankL2:ResourceMgr.badTankL, x, y, null);
                break;
            case UP:
                g.drawImage(this.getGroup()==Group.GOOD?random.nextInt(100)>50?ResourceMgr.goodTankU:ResourceMgr.goodTankU2:ResourceMgr.badTankU, x, y, null);
                break;
            case RIGHT:
                g.drawImage(this.getGroup()==Group.GOOD?random.nextInt(100)>50?ResourceMgr.goodTankR:ResourceMgr.goodTankR2:ResourceMgr.badTankR, x, y, null);
                break;
            case DOWN:
                g.drawImage(this.getGroup()==Group.GOOD?random.nextInt(100)>50?ResourceMgr.goodTankD:ResourceMgr.goodTankD2:ResourceMgr.badTankD, x, y, null);
                break;

        }
        move();
    }

    public void move() {

        if (random.nextInt(50) > 35) {
            this.moving = false;
        } else {
            this.moving = true;
        }

        if (!moving) return;
        switch (dir) {
            case LEFT:
                x -= SPPED;
                break;
            case UP:
                y -= SPPED;
                break;
            case DOWN:
                y += SPPED;
                break;
            case RIGHT:
                x += SPPED;
                break;
        }
        //超出边界，掉头
        reDir();
        //方向随机
        randomDir();
        tankRec.x = this.x;
        tankRec.y = this.y;
        if (random.nextInt(10) > 8)
            this.fire();
    }

    public void randomDir(){
        if (random.nextInt(100) >97 ) {
            this.dir = Dir.UP;
        }
        if (random.nextInt(100)>99 ) {
            this.dir = Dir.DOWN;
        }
        if (random.nextInt(100) >98) {
            this.dir = Dir.RIGHT;
        }

        if (random.nextInt(120) >118) {
            this.dir = Dir.LEFT;
        }
    }


    public void reDir(){
        if (this.x < this.WIDTH) {
            this.dir = Dir.RIGHT;
        }
        if (this.y < this.HEIGHT) {
            this.dir = Dir.DOWN;
        }
        if (this.x > TankFrame.GAME_WIDTH-this.WIDTH) {
            this.dir = Dir.LEFT;
        }
        if (this.y > TankFrame.GAME_HEIGHT-this.HEIGHT) {
            this.dir = Dir.UP;
        }
    }

    public void fire() {
//        int bulletX = this.x + Tank.WIDTH / 2 - Bullet.WIDTH / 2;
//        int BulletY = this.y + Tank.HEIGHT / 2 - Bullet.HEIGHT / 2;
//
//        tankFrame.bullets.add(new Bullet(bulletX, BulletY, this.dir, this.getGroup(), this.tankFrame));
        this.fs.fire(this);
    }


    public Dir getDir() {
        return dir;
    }

    public void setDir(Dir dir) {
        this.dir = dir;
    }

    public boolean isMoving() {
        return moving;
    }

    public void setMoving(boolean moving) {
        this.moving = moving;
    }


    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public void die() {
        this.living = false;
    }

    public Group getGroup() {
        return group;
    }

    public void setGroup(Group group) {
        this.group = group;
    }
}
